package com.shanpin.shebao.admin.module.move;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shanpin.core.constant.PayForConst;
import com.shanpin.shebao.admin.module.city.CityCacheService;
import com.shanpin.shebao.admin.module.move.model.MoveOrderQueryForm;
import com.shanpin.shebao.admin.module.move.model.MoveSettingList;
import com.shanpin.shebao.constant.QueryOperation;
import com.shanpin.shebao.model.CityMove;
import com.shanpin.shebao.model.ServiceResult;
import com.shanpin.shebao.model.order.MoveOrders;
import com.shanpin.shebao.utility.Criterition;
import com.shanpin.shebao.utility.MoreCriteria;
import com.shanpin.shebao.utility.PageModel;

@Service
public class MoveAdminService {
	

	@Autowired
	private MoveAdminDao moveAdminDao;
	@Autowired
	private CityCacheService cityCacheService;
	
	public List<MoveSettingList> list(int pcityCode, int max_pcityCode) {
		return moveAdminDao.list(pcityCode, max_pcityCode);
	}
	

	public CityMove load(int city_code) {
		return moveAdminDao.load(city_code);
	}
	
	public boolean setting(CityMove cityMove) {
		return moveAdminDao.setting(cityMove);
	}

	
	public PageModel orders(int pageSize, int pageNo, MoveOrderQueryForm form) {
		int start = (pageNo-1)*pageSize;
		PageModel pm = new PageModel();
		pm.setPageNo(pageNo);
		pm.setPageSize(pageSize);
		
		MoreCriteria<String, Criterition> mc = buildQuery(form);
		pm.setTotalRecord(moveAdminDao.orderQueryCount(mc));
		moveAdminDao.orders(start, pm, mc);
		buildOrders(pm);
		return pm;
	}
	
	private void buildOrders(PageModel pm) {
		List<MoveOrders> list = pm.getRecordList();
		for (MoveOrders mo : list) {
			mo.setType_name(mo.getMove_type() == PayForConst.SHEBAO.getNum() ? "社保" : "公积金");
			if (mo.getOut_city() > 0) {
				mo.setOut_cname(cityCacheService.get(mo.getOut_city()).getCity_name());
				mo.setType_name(mo.getType_name()+"转出");
			}
			if (mo.getInto_city() > 0) {
				mo.setInto_cname(cityCacheService.get(mo.getInto_city()).getCity_name());
				mo.setType_name(mo.getType_name()+"转入");
			}
		}
	}
	
	private MoreCriteria<String, Criterition> buildQuery(MoveOrderQueryForm form) {
		MoreCriteria<String, Criterition> mc = new MoreCriteria<String, Criterition>();
		Map<String, Criterition> pair = (Map<String, Criterition>) mc.getPair();
		if(StringUtils.isNotBlank(form.getMove_no())) {
			Criterition c = new Criterition("m.move_no", form.getMove_no().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("move_no", c);
		}
		if(StringUtils.isNotBlank(form.getUid())) {
			Criterition c = new Criterition("u.phone", form.getUid().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("phone", c);
		}
		if(StringUtils.isNotBlank(form.getMove_type())) {
			Criterition c = new Criterition("m.move_type", form.getMove_type().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("move_type", c);
		}
		if(StringUtils.isNotBlank(form.getOut_city())) {
			Criterition c = new Criterition("m.out_city", form.getOut_city().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("out_city", c);
		}
		if(StringUtils.isNotBlank(form.getInto_city())) {
			Criterition c = new Criterition("m.into_city", form.getInto_city().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("into_city", c);
		}
		if(StringUtils.isNotBlank(form.getStart_time())) {
			Criterition c = new Criterition("m.gen_time", form.getStart_time().trim(), QueryOperation.GE, QueryOperation.AND);
			pair.put("start_time", c);
		}
		if(StringUtils.isNotBlank(form.getEnd_time())) {
			Criterition c = new Criterition("m.gen_time", form.getEnd_time().trim()+" 23:59:59", QueryOperation.LE, QueryOperation.AND);
			pair.put("end_time", c);
		}
		if(StringUtils.isNotBlank(form.getStatus())) {
			Criterition c = new Criterition("m.status", form.getStatus().trim()+" 23:59:59", QueryOperation.EQ, QueryOperation.AND);
			pair.put("status", c);
		}
		return mc;
	}
	
	public List<Integer> getOutCitys() {
		return moveAdminDao.getOutCitys();
	}
	
	public List<Integer> getIntoCitys() {
		return moveAdminDao.getIntoCitys();
	}
	
	public boolean updateNotes(String move_no, String notes) {
		return moveAdminDao.updateNotes(move_no, notes);
	}
	

	public boolean refund(String move_no, String notes) {
		return moveAdminDao.refund(move_no, notes);
	}
	
	public boolean success(String move_no, String notes) {
		return moveAdminDao.success(move_no, notes);
	}
	
	
	public ServiceResult<List<Object>> export(MoveOrderQueryForm form) {
		MoreCriteria<String, Criterition> mc = buildQuery(form);
		int totalRecord = moveAdminDao.orderQueryCount(mc);
		if (totalRecord > 1000) {
			return ServiceResult.valueOf("数据量超1000条，不执行导出操作，请选择过滤。");
		}
		int start = 0;
		PageModel pm = new PageModel();
		pm.setTotalRecord(totalRecord);
		pm.setPageSize(totalRecord);
		moveAdminDao.orders(start, pm, mc);
		buildOrders(pm);
		List<Object> list = pm.getRecordList();
		return ServiceResult.valueT(list);
	}
	
}
